package com.itheima.test;

import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class testSecondLevelCache {

    private InputStream in;
    private  SqlSessionFactory factory;

    @Before //用于在测试方法之前执行
    public  void  init () throws Exception{
        //1.读取配置文件，生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取sqlSessionFactory
        factory = new SqlSessionFactoryBuilder().build(in);

    }
    @After //用于在测试方法之后执行
    public  void  destory() throws Exception{
        in.close();
    }
    /**
     * 测试一级缓存
     */
    @Test
    public void testFirstLevelCache(){
        SqlSession sqlsession1 = factory.openSession();
        IUserDao dao1 = sqlsession1.getMapper(IUserDao.class);
        User user1 = dao1.findById(41);
        System.out.println(user1);

        sqlsession1.close();

        SqlSession sqlsession2 = factory.openSession();
        IUserDao dao2 = sqlsession2.getMapper(IUserDao.class);
        User user2 = dao2.findById(41);
        System.out.println(user2);

        sqlsession2.close();

        System.out.println(user1==user2);
        System.out.println(user1.equals(user2));
    }




}
